# Create your views here.



from django.shortcuts import render_to_response,HttpResponseRedirect
from django.conf.urls.defaults import *
from django.contrib.auth import authenticate, login, logout
from django.template import RequestContext
from whileyouwere.main.models import PostForm, PostModel, Intro, Comment, CommentForm
from django.core.urlresolvers import reverse
from django.core.mail import send_mail
import datetime
import locale
import random

def index(request):  
    posts = PostModel.objects.filter(approve=True).order_by('-datestamp')
    if posts.count() > 10:
        next = 2
        posts = posts[0:10]
    else:
        next = 0
    
    intros = Intro.objects.all()
    length = intros.count()
    
    rand = random.randint(1,length)
    
    description = intros[rand-1]
    prev = 0
    return render_to_response('index.html',{'posts':posts,'next':next,'prev':prev,'desc':description},context_instance = RequestContext(request))

def post(request):
    response = 0
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            newpost = PostModel()
            newpost.adjective = form.cleaned_data['adjective']
            newpost.noun = form.cleaned_data['noun']
            newpost.whileyou = form.cleaned_data['whileyou']
            newpost.description = form.cleaned_data['description']
            newpost.gender = form.cleaned_data['gender']
            newpost.five = 0
            newpost.low = 0
            newpost.comments = 0
            newpost.datestamp = datetime.datetime.now()
            newpost.approve = False
            newpost.campus = form.cleaned_data['campus']
            newpost.campus_aff = form.cleaned_data['campus_aff']
            newpost.save()
           
            if not newpost.campus_aff:
                campus = newpost.campus
            else:
                campus = 'none'
           
            send_mail('New Post to be Approved', 'The following post has been submitted: \n\n'+newpost.gender+' about '+newpost.adjective+' '+newpost.noun+' at '+ campus + '\n\nwhile you were '+newpost.whileyou+'...\n\n'+newpost.description+'.\n\nTo approve this post, please click here: http://www.while-you-were.com/approve/'+str(newpost.id)+'/\n\nThank you,\nThe WhileYouWere Team', 'darwin_quark@while-you-were.com',
    ['whileyouwerewondering@gmail.com'], fail_silently=True)
            
            response = 1
            return render_to_response('post_your_confession.html',{'form':form,'response':response,'post':newpost},context_instance=RequestContext(request))
        else:
            return render_to_response('post_your_confession.html',{'form':form,'response':response},context_instance=RequestContext(request))
    else:
        form = PostForm()
        return render_to_response('post_your_confession.html',{'form':form,'response':response},context_instance=RequestContext(request))

def more(request,post_num):
    posts = PostModel.objects.order_by('-datestamp')
    post_num = int(post_num)
    count = posts.count()
    limit = 10*post_num
    if  (count > limit):
        next = post_num+1
        posts=posts[10*(post_num-1):10*post_num]
    else:
        posts=posts[10*(post_num-1):posts.count()]
        next = 0

    prev = post_num-1
    return render_to_response('index.html',{'posts':posts,'next':next,'prev':prev},context_instance = RequestContext(request))

def teaser(request):
    return render_to_response('teaser.html',context_instance = RequestContext(request))

def high(request,post_num):
    if not post_num in request.session or not request.session.get(post_num,True):
        post = PostModel.objects.get(pk=post_num)
        post.five = post.five+1
        post.save()
        request.session[post_num]=True
        
        return render_to_response("highlow.html",{'value': post.five},context_instance=RequestContext(request))
    else:
        post = PostModel.objects.get(pk=post_num) 
        return render_to_response("highlow.html",{'value': post.five},context_instance=RequestContext(request))

def low(request,post_num):
    if not post_num in request.session or not request.session.get(post_num,True):
        post = PostModel.objects.get(pk=post_num)
        post.low = post.low+1
        post.save()
        request.session[post_num]=True
        
        return render_to_response("highlow.html",{'value': post.low},context_instance=RequestContext(request))
    else:
        post = PostModel.objects.get(pk=post_num) 
        return render_to_response("highlow.html",{'value': post.low},context_instance=RequestContext(request))


def fame(request):  
    posts = PostModel.objects.filter(approve=True).order_by('-five')
    if posts.count() > 5:
        posts = posts[0:5]
    else:
        posts = posts[0:posts.count()]
    
    return render_to_response('high_rated.html',{'posts':posts},context_instance = RequestContext(request))

def shame(request):  
    posts = PostModel.objects.filter(approve=True).order_by('-low')
    if posts.count() > 5:
        posts = posts[0:5]
    else:
        posts = posts[0:posts.count()]
    
    return render_to_response('low_rated.html',{'posts':posts},context_instance = RequestContext(request))

def about(request):
    return render_to_response('about_us.html',context_instance = RequestContext(request))

def approve(request,post_num):
    post = PostModel.objects.get(pk=post_num)
    post.approve = True
    post.save()
    
    return HttpResponseRedirect(reverse('whileyouwere.main.views.index'))

def removec(request,comm_num):
    comment = Comment.objects.get(pk=comm_num)
    if comment.approve == True:
        comment.approve = False
        post = PostModel.objects.get(pk=comment.post)
        post.comments = post.comments - 1
        post.save()
        comment.save()
    
    return HttpResponseRedirect(reverse('whileyouwere.main.views.index'))
def wyw(request,post_num):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            newcomment = Comment()
            newcomment.author = form.cleaned_data['author']
            newcomment.comment = form.cleaned_data['comment']
            newcomment.post = post_num
            newcomment.datestamp = datetime.datetime.now()
            newcomment.approve = True
            newcomment.save()
           
            post = PostModel.objects.get(pk=post_num)
            post.comments = post.comments+1
            post.save()
            
            send_mail('New Comment to be Approved', 'A comment by ' + newcomment.author+' has been posted: \n\n' + newcomment.comment + '\n\n ...in reference to the following post: \n\n'+post.gender+' about '+post.adjective+' '+post.noun+'\n\nwhile you were '+post.whileyou+'...\n\n'+post.description+'.\n\nTo remove this comment, please click here: http://www.while-you-were.com/removec/'+str(newcomment.id)+'/\n\nThank you,\nThe WhileYouWere Team', 'darwin_quark@while-you-were.com',
    ['whileyouwerewondering@gmail.com'], fail_silently=True)
            return HttpResponseRedirect(reverse('whileyouwere.main.views.wyw',args=(post_num,)))
        else:
            return HttpResponseRedirect(reverse('whileyouwere.main.views.wyw',args=(post_num,)))
   
    else: 
        post = PostModel.objects.get(pk=post_num)
    
        comments = Comment.objects.filter(post=post_num,approve=True).order_by('datestamp')
        form = CommentForm()
        return render_to_response('post.html',{'post': post,'comments':comments,'form':form},context_instance = RequestContext(request))


    
